package sweetguy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import sweetguy.dto.RoleInsertDTO;
import sweetguy.dto.RoleUpdateDTO;
import sweetguy.dto.PageDTO;
import sweetguy.entity.dto.Role;
import sweetguy.exception.ServiceException;
import sweetguy.mapper.RoleMapper;
import sweetguy.service.RoleService;

import java.time.LocalDateTime;
import java.util.List;

@Service
@CacheConfig(cacheNames = "role")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @CacheEvict(allEntries = true)
    public int insert(RoleInsertDTO roleInsertDTO) {
        Role role = BeanUtil.copyProperties(roleInsertDTO, Role.class);
        // 设置默认值
        role.setId(null);
        role.setDeleted(0);
        role.setCreated(LocalDateTime.now());
        role.setUpdated(LocalDateTime.now());
        // DB添加
        int result = roleMapper.insert(role);
        if (result <= 0) {
            throw new RuntimeException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null", unless = "#result == null ")
    @Override
    public Role selectById(Long id) {
        Role result = roleMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(RoleUpdateDTO roleUpdateDTO) {
        Role role = BeanUtil.copyProperties(roleUpdateDTO, Role.class);
        // 设置默认值
        role.setCreated(null);
        role.setUpdated(LocalDateTime.now());
        // DB修改
        int result = roleMapper.updateById(role);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //todo 查询该记录是否被引用
        int result = roleMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //todo
        int result = roleMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批量删除失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<Role> list() {
        return roleMapper.list();
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#pageDTO.getPageNum() + ':' + " +
            "#pageDTO.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Role> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new PageInfo<>(roleMapper.list());
    }
}